using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

namespace OnlineAnadolu
{
    public partial class ME : System.Web.UI.Page
    {
        Database db = new Database();
        protected void Page_Load(object sender, TreeNodeEventArgs e)
        {
            if (!IsPostBack)
            {
             
             //   FillAuthors(
 
            }
         

        }
        void FillAuthors(MenuItem node)
        {
            string connString = System.Configuration.ConfigurationSettings.AppSettings["MyCon"];
            SqlConnection connection = new SqlConnection(connString);
            SqlCommand command = new SqlCommand("Select * From categories where parentid is null", connection);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet authors = new DataSet();
            adapter.Fill(authors);
            if (authors.Tables.Count > 0)
            {
                foreach (DataRow row in authors.Tables[0].Rows)
                {
                    MenuItem newNode = new MenuItem();
                    newNode.Text = row["categoryname"].ToString();
                    newNode.Value = (row["categoryid"]).ToString();

                   // newNode.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();

                    // newNode.SelectAction = TreeNodeSelectAction.Expand;
                    node.ChildItems.Add(newNode);

                    FillTitlesForAuthors(newNode);

                }
            }
        }

        void FillTitlesForAuthors(MenuItem node)
        {
            string authorID = node.Value.ToString();
            string connString = System.Configuration.ConfigurationSettings.AppSettings["MyCon"];
            SqlConnection connection = new SqlConnection(connString);
            SqlCommand command = new SqlCommand("Select categoryname, categoryid From categories Where parentid = '" + authorID + "'", connection);
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet titlesForAuthors = new DataSet();
            adapter.Fill(titlesForAuthors);
            if (titlesForAuthors.Tables.Count > 0)
            {
                foreach (DataRow row in titlesForAuthors.Tables[0].Rows)
                {
                    MenuItem newNode = new MenuItem();
                    newNode.Text = row["categoryname"].ToString();
                    newNode.Value = (row["categoryid"]).ToString();
                //    newNode.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();

                   // newNode.PopulateOnDemand = false;
                 //   newNode.SelectAction = TreeNodeSelectAction.None;
                    node.ChildItems.Add(newNode);
                    //node.NavigateUrl = "../UrunGoster.aspx?CatID=" + (row["categoryid"]).ToString();

                    FillTitlesForAuthors(newNode);
                }
            }
        }
        void click()
        {
            Response.Write(Menu1.SelectedItem.Text);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillAuthors(Menu1.Items[0]);
            }
        }
        int yolbul(int childid)
        {
            string query1 = "Select * from categories where categoryid=" + childid + "";
            DataTable dt = db.select(query1);
            
            foreach(DataRow dr in dt.Rows)
            {         
                Label1.Text = ">>>"+dr["categoryname"].ToString() + Label1.Text;
                string query2 = "SELECT     attributetypes.datatype, attributes.attributename, attributevalues.value, attributes.categoryid FROM attributes INNER JOIN attributevalues ON attributes.attributeid = attributevalues.attributeid INNER JOIN attributetypes ON attributes.type = attributetypes.attributecode WHERE(attributes.categoryid = "+dr["categoryid"].ToString()+")";
                DataTable dt2 = new DataTable();
                dt2 = db.select(query2);
                if (dt2.Rows.Count != 0)
                {
                    Label lr = new Label();
                    foreach(DataRow dr2 in dt2.Rows)
                    {
                        lr.Text = dr2["attributename"].ToString();
                        Panel1.Controls.Add(lr);
                       

                        DropDownList dwn2 = new DropDownList();
                        string query3 = "SELECT     attributevalues.value FROM attributes INNER JOIN attributevalues ON attributes.attributeid = attributevalues.attributeid INNER JOIN attributetypes ON attributes.type = attributetypes.attributecode WHERE(attributes.categoryid = " + dr["categoryid"].ToString() + ")";
                        DataTable dt3 = new DataTable();
                        dt3 = db.select(query3);
                        dwn2.DataTextField = "value";
                        dwn2.DataSource=dt3;
                        dwn2.DataBind();
                        Panel1.Controls.Add(dwn2);
                        
                    }
                }
                if (dr["parentid"].ToString()=="")
                    return 0;

                yolbul(Convert.ToInt32(dr["parentid"]));
            }

            return 0;

        }
        protected void click(object sender, MenuEventArgs e)
        {
            Label1.Text = "";
            string query = "Select * from categories where parentid="+Menu1.SelectedValue+"";

            DataTable dt = db.select(query);
            if (dt.Rows.Count != 0)
                Label1.Text = "Kategorilendirmede sonuna kadar gitmediniz";
            else
            {
               // Label1.Text = " "+ Menu1.SelectedItem.Text+Label1.Text;
                yolbul(Convert.ToInt32(Menu1.SelectedValue));
            }
            
            //Response.Write(Menu1.SelectedItem.Text);
            


        }
        
      
      
      


    
    }
}
